#delimit ;

set more 1;


log using bjps_birthorder_regs, t replace;

*2013 election;

use data\sample2013, clear;
format vote %8.3f;
tab order, sum(vote);

eststo t1c1: reghdfe vote i.order i.sex, absorb(morid byr) cl(morid);
sum vote if e(sample); local mt1c1= r(mean);

eststo ta1c1: reghdfe vote i.college i.sex, absorb(morid byr) cl(morid);
sum vote if e(sample); local mta1c1= r(mean);

eststo ta2c1: reghdfe vote i.order if  sex, absorb(morid byr) cl(morid);
sum vote if e(sample); local mta2c1= r(mean);
eststo ta2c2: reghdfe vote i.order if !sex, absorb(morid byr) cl(morid);
sum vote if e(sample); local mta2c2= r(mean);

eststo ta5c1: reghdfe vote i.order i.sex if par_divorced==0, absorb(morid byr) cl(morid);
sum vote  if e(sample); local mta5c1= r(mean);

eststo ta6ac1: reghdfe vote i.order i.sex if sibsize==2, absorb(morid byr) cl(morid);
sum vote  if e(sample); local mta6ac1= r(mean);
eststo ta6ac2: reghdfe vote i.order i.sex if sibsize==3, absorb(morid byr) cl(morid);
sum vote  if e(sample); local mta6ac2= r(mean);
eststo ta6ac3: reghdfe vote i.order i.sex if sibsize==4, absorb(morid byr) cl(morid);
sum vote  if e(sample); local mta6ac3= r(mean);
eststo ta6ac4: reghdfe vote i.order i.sex if sibsize==5, absorb(morid byr) cl(morid);
sum vote  if e(sample); local mta6ac4= r(mean);

sort morid byr;
g tmp1= byr-byr[_n-1] if sibsize==2 & morid==morid[_n-1];
egen agediff= mean(tmp1), by(morid);
drop tmp1;

eststo ta8c1: reghdfe vote i.order i.sex if sibsize==2 & agediff<=3, absorb(morid byr) cl(morid);
sum vote  if e(sample); local mta8c1= r(mean);
eststo ta8c2: reghdfe vote i.order i.sex if sibsize==2 & agediff>3 & agediff<., absorb(morid byr) cl(morid);
sum vote  if e(sample); local mta8c2= r(mean);

g byte par_coll= mor_smcoll*far_smcoll;
replace par_coll= 0 if par_coll==.;

eststo ta9c1: reghdfe vote i.order i.sex if par_coll==0, absorb(morid byr) cl(morid);
sum vote  if e(sample); local mta9c1= r(mean);
eststo ta9c2: reghdfe vote i.order i.sex if par_coll==1, absorb(morid byr) cl(morid);
sum vote  if e(sample); local mta9c2= r(mean);

g byte dum= 1;
g byte first= order==1;

eststo ta11ac1: reghdfe vote i.first i.sex, absorb(byr) cl(morid);
sum vote  if e(sample); local mta11ac1= r(mean);
eststo ta11bc1: reghdfe vote i.first i.sex, absorb(morid byr) cl(morid);
sum vote  if e(sample); local mta11bc1= r(mean);


*2015 election;

use data\sample2015, clear;
format vote %8.3f;
tab order, sum(vote);

eststo t1c2: reghdfe vote i.order i.sex, absorb(morid byr) cl(morid);
sum vote  if e(sample); local mt1c2= r(mean);

eststo ta1c2: reghdfe vote i.college i.sex, absorb(morid byr) cl(morid);
sum vote  if e(sample); local mta1c2= r(mean);

eststo ta2c3: reghdfe vote i.order i.sex if sex, absorb(morid byr) cl(morid);
sum vote  if e(sample); local mta2c3= r(mean);
eststo ta2c4: reghdfe vote i.order i.sex if !sex, absorb(morid byr) cl(morid);
sum vote  if e(sample); local mta2c4= r(mean);

eststo ta5c2: reghdfe vote i.order i.sex if par_divorced==0, absorb(morid byr) cl(morid);
sum vote  if e(sample); local mta5c2= r(mean);

eststo ta6bc1: reghdfe vote i.order i.sex if sibsize==2, absorb(morid byr) cl(morid);
sum vote  if e(sample); local mta6bc1= r(mean);
eststo ta6bc2: reghdfe vote i.order i.sex if sibsize==3, absorb(morid byr) cl(morid);
sum vote  if e(sample); local mta6bc2= r(mean);
eststo ta6bc3: reghdfe vote i.order i.sex if sibsize==4, absorb(morid byr) cl(morid);
sum vote  if e(sample); local mta6bc3= r(mean);
eststo ta6bc4: reghdfe vote i.order i.sex if sibsize==5, absorb(morid byr) cl(morid);
sum vote  if e(sample); local mta6bc4= r(mean);

sort morid byr;
g tmp1= byr-byr[_n-1] if sibsize==2 & morid==morid[_n-1];
egen agediff= mean(tmp1), by(morid);
drop tmp1;

eststo ta8c3: reghdfe vote i.order i.sex if sibsize==2 & agediff<=3, absorb(morid byr) cl(morid);
sum vote  if e(sample); local mta8c3= r(mean);
eststo ta8c4: reghdfe vote i.order i.sex if sibsize==2 & agediff>3 & agediff<., absorb(morid byr) cl(morid);
sum vote  if e(sample); local mta8c4= r(mean);

g byte par_coll= mor_smcoll*far_smcoll;
replace par_coll= 0 if par_coll==.;

eststo ta9c3: reghdfe vote i.order i.sex if par_coll==0, absorb(morid byr) cl(morid);
sum vote if e(sample); local mta9c3= r(mean);
eststo ta9c4: reghdfe vote i.order i.sex if par_coll==1, absorb(morid byr) cl(morid);
sum vote if e(sample); local mta9c4= r(mean);

g byte dum= 1;
g byte first= order==1;

eststo ta11ac2: reghdfe vote i.first i.sex, absorb(byr) cl(morid);
sum vote if e(sample); local mta11ac2= r(mean);
eststo ta11bc2: reghdfe vote i.first i.sex, absorb(morid byr) cl(morid);
sum vote if e(sample); local mta11bc2= r(mean);

esttab t1c*, keep(*.order) se star(* 0.1 ** 0.05 *** 0.01) b(3) compress;
di "means   " `mt1c1' "  " `mt1c2';

esttab ta1c*, keep(1.college) se star(* 0.1 ** 0.05 *** 0.01) b(3) compress;
di "means   " `mta1c1' "  " `mta1c2';

esttab ta2c*, keep(*.order) se star(* 0.1 ** 0.05 *** 0.01) b(3) compress;
di "means   " `mta2c1' "  " `mta2c2' "  "  `mta2c3' "  " `mta2c4';

esttab ta5c*, keep(*.order) se star(* 0.1 ** 0.05 *** 0.01) b(3) compress;
di "means   " `mta5c1' "  " `mta5c2';

esttab ta6ac*, keep(*.order) se star(* 0.1 ** 0.05 *** 0.01) b(3) compress;
di "means   " `mta6ac1' "  " `mta6ac2' "  "  `mta6ac3' "  " `mta6ac4';

esttab ta6bc*, keep(*.order) se star(* 0.1 ** 0.05 *** 0.01) b(3) compress;
di "means   " `mta6bc1' "  " `mta6bc2' "  "  `mta6bc3' "  " `mta6bc4';

esttab ta8c*, keep(*.order) se star(* 0.1 ** 0.05 *** 0.01) b(3) compress;
di "means   " `mta8c1' "  " `mta8c2' "  "  `mta8c3' "  " `mta8c4';

esttab ta9c1 ta9c2 ta9c3 ta9c4, keep(*.order) se star(* 0.1 ** 0.05 *** 0.01) b(3) compress;
di "means   " `mta9c1' "  " `mta9c2' "  "  `mta9c3' "  " `mta9c4';

esttab ta11ac*, keep(1.first) se star(* 0.1 ** 0.05 *** 0.01) b(3) compress;
di "means   " `mta11ac1' "  " `mta11ac2';

esttab ta11bc*, keep(1.first) se star(* 0.1 ** 0.05 *** 0.01) b(3) compress;
di "means   " `mta11bc1' "  " `mta11bc2';

log close;


************ Additional results ***************
* 2013
* Conditional logit regressions

clogit vote i.ip_birthyear female bo_2-bo_5 , group(fammor) cl(fammor)
 margins , dydx(bo_2-bo_5) predict(pu0) post
 estadd local Controls "Family f.e"
  summarize voted13 if e(sample)==1 & bo_1==1
estadd scalar mean_depvar = r(mean)
eststo v13_fe

* mediation results for Figure 3
areg vote i.ip_birthyear female    bo_2-bo_5   , absorb(fammor) cl(fammor)
  est store m1_13
  areg vote i.ip_birthyear female    bo_2-bo_5  rank_kltrinn_2013 rank_3bef13 , absorb(fammor) cl(fammor)
  est store m2_13

* 2015
* Conditional logit regressions
clogit vote i.ip_birthyear female bo_2-bo_5 , group(fammor) cl(fammor)
 margins , dydx(bo_2-bo_5) predict(pu0) post
 estadd local Controls "Family f.e"
  summarize voted15 if e(sample)==1 & bo_1==1
estadd scalar mean_depvar = r(mean)
eststo v15_fe

* mediation results for Figure 3
  set more off
areg vote i.ip_birthyear female  bo_2-bo_5 , absorb(fammor) cl(fammor)
  est store m1_15
areg vote i.ip_birthyear female   bo_2-bo_5  rank_kltrinn_2015 rank_3bef15  , absorb(fammor) cl(fammor)
  est store m2_15
